#pragma once

namespace sqlite3x { class connection; }

struct ClientInfo;
class Dealing;
class Position;

//for database
class Memory		
{
public:
 	Memory(sqlite3x::connection& connection);
	~Memory();

	void Save(int table, void const* data, char const* sql);
	void* Load(char const* sql);

	void Save(Dealing const& data);
	void Save(Position const& data);

private:
	void OnCustomer();

private:
	void CreateTable(char const* tableName, char const* createSQl);

 	bool IsExist(int table, char const* key) const;

	void Insert(int table, void const* data, char const* sql);
	void Update(int table, void const* data, char const* sql);

private:
	sqlite3x::connection& m_connection;
};


//for internal business
class Dealing;
class DealingCache
{
public:
	static DealingCache& Instance();

	DealingCache();
	~DealingCache();
	Dealing& Save(Dealing const& dealing);
	Dealing* Get(int localId);
private:
	map<int,Dealing> m_dealingMap;
};

class OrderAction;
class OrderActionCache
{
public:
	static OrderActionCache& Instance();
	OrderActionCache();
	~OrderActionCache();
	OrderAction& Save(OrderAction const& action);
	OrderAction const* Get(int actionId) const;
	
private:
	map<int, OrderAction> m_orderActionMap;
};

class BusinessDealingCache
{
public:
	void Clear() { m_dealings.clear(); }
	void Add(Dealing const& dealing) { m_dealings.push_back(&dealing); }
	bool IsEmpty() const { return m_dealings.empty(); }
	Dealing const& Last() const	{ return *m_dealings.back(); }
private:
	std::vector<Dealing const*> m_dealings;
};
